package com.wangyk.gateway.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.apache.commons.lang.StringUtils;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.util.UUID;

import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.POST_TYPE;
import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.SEND_RESPONSE_FILTER_ORDER;

/**
 * gateway的过滤器
 */
@Component
public class AddResponseHeaderFilter extends ZuulFilter
{
    /**
     * 设置过滤器的类型
     */
    public String filterType()
    {
        return POST_TYPE;
    }

    public int filterOrder()
    {
        return SEND_RESPONSE_FILTER_ORDER-1;
    }

    public boolean shouldFilter()
    {
        return true;
    }

    public Object run()
        throws ZuulException
    {
        /**
         * 获取当前请求的上下文
         * */
        RequestContext currentContext = RequestContext.getCurrentContext();
        /**
         * 从上下文中获取请求
         * */
        HttpServletResponse response = currentContext.getResponse();
        response.setHeader("X-Foo", String.valueOf(UUID.randomUUID()));
        return null;
    }
}
